package com.whfc.ms.api.fse.service.impl;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.fse.dto.FseLiftDTO;
import com.whfc.fse.dto.FseLiftFvsDeviceDTO;
import com.whfc.fse.param.*;
import com.whfc.fse.service.FseLiftService;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.service.FvsDeviceService;
import com.whfc.ms.api.fse.service.MsFseLiftService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @DESCRIPTION
 * @AUTHOR GuoDong_Sun
 * @DATE 2020/4/10
 */
@Service
public class MsFseLiftServiceImpl implements MsFseLiftService {

    @DubboReference(interfaceClass = FseLiftService.class, version = "1.0.0")
    private FseLiftService fseLiftService;

    @DubboReference(interfaceClass = FvsDeviceService.class, version = "1.0.0")
    private FvsDeviceService fvsDeviceService;

    @Override
    public PageData<FseLiftDTO> list(Integer pageNum, Integer pageSize, Integer deptId, String keyword, Integer bindFlag, Integer netState) {
        return fseLiftService.list(pageNum, pageSize, deptId, keyword, bindFlag, netState);
    }

    @Override
    public void add(FseLiftAddParam request) {
        fseLiftService.add(request);
    }

    @Override
    public void edit(FseLiftEditParam request) {
        fseLiftService.edit(request);
    }

    @Override
    public void del(Integer id) {
        fseLiftService.del(id);
    }

    @Override
    public void bind(FseLiftBindParam request) {
        fseLiftService.bind(request);
    }

    @Override
    public void unbind(FseLiftUnbindParam param) {
        fseLiftService.unbind(param);
    }

    @Override
    public ListData<FseLiftDTO> list(Integer deptId, Integer bindFlag) {
        return fseLiftService.list(deptId, bindFlag);
    }

    @Override
    public FseLiftDTO detail(Integer liftId) {
        return fseLiftService.detail(liftId);
    }

    @Override
    public List<FseLiftFvsDeviceDTO> fvsList(Integer liftId) {
        List<FseLiftFvsDeviceDTO> list = fseLiftService.getFvsDeviceList(liftId);
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }
        //取出设备ID 查找监控设备信息
        Map<Integer, FseLiftFvsDeviceDTO> map = new HashMap<>();
        for (FseLiftFvsDeviceDTO fseLiftFvsDeviceDTO : list) {
            map.put(fseLiftFvsDeviceDTO.getFvsDeviceId(), fseLiftFvsDeviceDTO);
        }
        List<FvsDeviceDTO> fvsDeviceList = fvsDeviceService.list(new ArrayList<>(map.keySet()));
        for (FvsDeviceDTO fvsDeviceDTO : fvsDeviceList) {
            FseLiftFvsDeviceDTO fseLiftFvsDeviceDTO = map.get(fvsDeviceDTO.getId());
            if (fseLiftFvsDeviceDTO != null) {
                fseLiftFvsDeviceDTO.setFvsDevice(fvsDeviceDTO);
            }
        }
        return new ArrayList<>(map.values());
    }

    @Override
    public void bindFvs(FseLiftFvsParam request) {
        fseLiftService.bindFvs(request);
    }
}
